iT邦幫忙

2025 iThome 鐵人賽

DAY 1
0

剛好 Eloquent Javascript 讀到一半,剛好趁鐵人賽開賽逼自己看有沒有機會嗑完XDD(?)
因為這是個人的閱讀筆記,所以細節的部分不會列的很精細,只會挑重點寫唷~

希望不要沒幾天就陣亡rrr

數字

位元與二進位制

所有的資料都是以位元的型態存在電腦裡,儲存為ㄧ長串的序列。
任何具有兩個值的東西都可以用來表示位元(bit),通常是使用0跟1

快速複習一下
01 為 1,而 10 在位元只有兩個數字下就是代表2,依此類推

那麼 0101010 是什麼?
答:42

1 0 1 0 1 0
32 16 8 4 2 1

32+8+2=42

在js中表達整數跟小數點

就直接輸入數字
12 像這樣

小數點也很簡單 記得打小數點就好 77.5
e代表指數,可以代表指數次方

3e3 即為 3x10的3次方 = 3000

特殊數字 Infinity, -Infinity, NaN

Infinity = 正無限大
-Infinity = 負無限大
NaN = 用來表示這個值『不是數字』,像是0/0,Infinity - Infinity之類的
另外 NaN === NaN 會是 false

判斷是不是數字且排除 Infinity,-Infinity 之類的怪東西

typeof value === "number" && Number.isFinite(value)

判斷是不是NaN

Number.isNaN()

運算子

主要用來計算,加減乘除(+ 跟 - 跟 * 跟 /),還有一個取餘數(%)
另外 typeof 也是運算子,會以字串表示給定資料值的型態名稱

字串

表達字串有以下幾種方法

`Today is Monday.`
"String"
'Hello world'

字串相加即是連接字串
"Today is "+ "Monday" + "."
則會變成
"Today is Monday."

template literal 嵌入其他資料值

`half of 100 is ${100/2}`

Boolean

true 跟 false

空值 null & undefined

  • Never set vairables to undefined
  • 未被宣告/未被定義的變數會是undefined
  • null 的意思就是 no object

檢查x是否為空值

if (x === null || x === undefined){
    ...
}

或直接用falsy value排除(null & undefined都是falsy value)

if(!x){
    ...
}

比較值是否相等

請善用 === 跟 !==

Short-circuiting

&& 跟 ||
|| returns first truthy operand, or the last falsy operand

let userName = "guest";
console.log(null || userName); // "guest"
console.log( "host" || userName); // "host"

&& returns first falsy operand. or the last truthy operand

Falsy Value In Javascript

  • ""(empty string)
  • 0 (zero)
  • false
  • null
  • undefined
  • NaN (Not a number)

Nullish Coalescing Operator (??=)

當左側變數為 undefined 或 null 則 assign 右側 default 值給左側變數
若左側變數不為 undefined 或 null 則左側變數值維持不變

if(x === null || x === undefined){
    x = y;
}

// 等同於
x ??= y

下一篇
Chapter 2 程式結構-day2
系列文
溫故而知新:Eloquent Javascript 閱讀筆記30
圖片
  熱門推薦
圖片
{{ item.channelVendor }} | {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

1 則留言

0
Wolke
iT邦研究生 4 級 ‧ 2025-10-10 18:23:55

感謝 未知作者 的精彩分享!

JavaScript 生態系統真的很豐富,這樣的分享對開發者很有幫助。

也歡迎版主有空參考我的系列文「南桃AI重生記」:https://ithelp.ithome.com.tw/users/20046160/ironman/8311

如果覺得有幫助的話,也歡迎訂閱支持!

我要留言

立即登入留言